/**
 * 配件仓库维护 编辑页面
 * @author mengyb
 * @date 2019/04/10
 */
define(function () {
    var self,
        app = win.app,
        doc = win.doc,
        vueData = {
            isAdd: false,
            isDetail: false,
            form: '',
            rules: {
                GWAS001: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWASL003: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWASL004: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWAS002_NAME: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWASACTI: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ]
            },
            selectOptions: {
                sys_y_n: app.datas_("sys_y_n")
            }
        },
        orgsList = [],
        callobj,
        layOpenIndex,
        moduleName,
        pageVue
    ;


    var Gwai600Edit = app.Module.extend({

        //初始化
        init: function () {
            self = this;
            self._super({
                _tag: 'gwai600_edit',
                _loadTplsFull: [
                    {
                        dom: 'common_open_window', tpl: 'common/_open_window'
                    }
                ]
            }, win);
        },

        //开窗后回调函数
        _call: function (tagData) {
            pageVue.$refs['form'].resetFields();
            moduleName = tagData.moduleName;
            layOpenIndex = tagData.layOpenIndex;

            if(tagData.is_detail){
                vueData.isDetail = true;
            }else {
                vueData.isDetail = false;
            }

            if (tagData.isAdd) {
                vueData.isAdd = true;
                app.extendUpLow({
                    target: vueData.form,
                    res: {
                        GWAS001: '',
                        GWASL003: '',
                        GWASL004: '',
                        GWAS002_NAME: '',
                        GWASACTI: 'Y'
                    },
                    reset: true
                });
            } else {
                vueData.isAdd = false;
                callobj = tagData.obj;
                app.extendUpLow({
                    target: vueData.form,
                    res: callobj.data,
                    reset: true
                });
            }
        },

        //新增
        add: function () {
            app.ajax({
                xurl: 'base/gwai600/add',
                xdata: vueData.form,
                loading: true,
                okmsg: moduleName + '信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule("gwai600").reloadTableCurr();
                        layer.close(layOpenIndex);
                    }

                }
            })
        },

        //修改
        edit: function () {
            app.ajax({
                xurl: 'base/gwai600/edit',
                xdata: vueData.form,
                loading: true,
                okmsg: moduleName + '信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        callobj.update(result.xdata);
                        layer.close(layOpenIndex);
                    }
                }
            })
        },

        //DOM加载完成后调用
        _ready: function () {
            vueData.form = app.fields_(['base_gwai600']);
            //实例化vue
            pageVue = new Vue({
                el: '#gwai600_edit',
                data: vueData,
                methods: {
                    //关闭窗口
                    cancle: function () {
                        layer.close(layOpenIndex);
                    },
                    //保存
                    save: function () {
                        //表单校验
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                if (vueData.isAdd) {
                                    self.add();
                                } else {
                                    self.edit();
                                }
                            }

                        });

                    },
                    //校验管理组织是否存在
                    // check: function () {
                    //     if (vueData.form.GWAS002_NAME != '') {
                    //         var obj = {};
                    //         obj = orgsList.find(function (item) {
                    //
                    //             return item.NAME === vueData.form.GWAS002_NAME;
                    //
                    //         });
                    //         if (typeof obj == "undefined") {
                    //             vueData.form.GWAS002 = '';
                    //             vueData.form.GWAS002_NAME = '';
                    //         } else {
                    //             vueData.form.GWAS002 = obj.ID;
                    //         }
                    //     } else {
                    //         vueData.form.GWAS002 = '';
                    //         vueData.form.GWAS002_NAME = '';
                    //     }
                    // },

                    //管理组织开窗
                    openOrgs: function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method:'q_sysorg_id',
                                    columnList: [
                                        {label: '组织编号', prop: 'ID', width: '150'},
                                        {label: '组织名称', prop: 'NAME', width: '150'}
                                    ],
                                    // url: 'crm/gwei009/listGwakPageC',
                                },
                                cb: function (data) {
                                    vueData.form.GWAS002 = data.ID;
                                    vueData.form.GWAS002_NAME = data.NAME;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择管理组织',
                                area: ['600px', '465px']
                            }
                        });
                    }
                },

                //使用ElementUI初始化DOM
                created: function () {
                    self._setEleIsReady('gwai600_edit');
                }
            })

        },

        _lazy: function () {
            // app.ajax({
            //     xurl: 'base/gwai600/getOrgsList',
            //     cb: function (result) {
            //         orgsList = result.data.list;
            //
            //     }
            // })
            this._super();
        }

    });


    return new Gwai600Edit();

});
